package csc.traning.wpsj.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.Session;

import com.sun.org.apache.xml.internal.security.Init;

import csc.traning.wpsj.dao.Counter;
import csc.traning.wpsj.dao.EmployeesDB;
import csc.traning.wpsj.dao.User;
import csc.traning.wpsj.dao.UserDB;
import csc.traning.wpsj.servlet.lessons.CookieUtilities;

/**
 * Servlet implementation class Logon
 * 
 * @author hdang20
 */
public class SubmitEditProfile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SubmitEditProfile() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<String> error = new ArrayList<String>();
		String nav = null;
		String cellPhone = request.getParameter("cellPhone");
		String email = request.getParameter("email");
		String firstName = request.getParameter("firstName");
		String lastName = request.getParameter("lastName");
		String newPassword = request.getParameter("newPassword");
		String confirmPassword = request.getParameter("confirmPassword");
		String oldPassword = request.getParameter("oldPassword");
		String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
				+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		Pattern pattern = Pattern.compile(EMAIL_PATTERN);
		Matcher matcher;

		if ("".equals(firstName))
			error.add("FirstName field is missing");
		if ("".equals(email))
			error.add("Email field is missing");
		else {
			matcher = pattern.matcher(email);
			if (!matcher.matches())
				error.add("Wrong email format");
		}
		if ("".equals(lastName))
			error.add("LastName field is missing");
		if ("".equals(oldPassword))
			error.add("Password field is missing");
		if (!"".equals(confirmPassword) || !"".equals(newPassword))
			if (!newPassword.equals(confirmPassword))
				error.add("New password and confirm new password is not the same");

		if (error.size() == 0) {
			HttpSession sess = request.getSession();
			User u = (User) sess.getAttribute("user");
			if (u != null) {
				User u2 = UserDB.getUser(u.getUserName(), u.getPassword());
				u2.setCellphone((String) request.getParameter("cellPhone"));
				u2.setEmailAddress((String) request.getParameter("email"));
				u2.setFirstName((String) request.getParameter("firstName"));
				u2.setLastName((String) request.getParameter("lastName"));
				String pass = (String) request.getParameter("newPassword");

				if (!"".equals(pass)) {
					u2.setPassword(pass);
				}
				sess.setAttribute("user", u2);

			}
			nav = "pages/file_management.jsp";
		} else {
			request.setAttribute("listError", error);
			nav = "pages/profile_editor.jsp";
		}
		RequestDispatcher dispatcher = request.getRequestDispatcher(nav);
		dispatcher.forward(request, response);
	}

}
